#!/usr/bin/python
# @author : Fabien Bagard

import re

def resplit(input, regex):
    print("regex.split(%s)" % input)
    s = regex.split(input)
    if s:
        print("\t%s" % s)
    else:
        print("\tNo match" % (input))
    print

def refindall(input, regex):
    print("regex.findall(%s)" % input)
    s = regex.findall(input)
    if s:
        print("\t%s" % s)
    else:
        print("\tNo match" % (input))
    print

def refinditer(input, regex):
    print("regex.finditer(%s)" % (input))
    s = regex.finditer(input)
    if s:
        for i in s:
            print "\tgroup(): ",
            print i.group()
            print "\tgroups(): ",
            print i.groups()
    else:    
        print("No match" % (input))
    print

def resub(input, repl, regex):
    print("regex.sub(%s)" % (input))
    s = regex.sub(repl, input)
    if s:
        print("\t%s" % s)
    else:    
        print("\tNo match" % (input))
    print

def resubn(input, repl, regex):
    print("regex.subn(%s)" % (input))
    s = regex.subn(repl, input)
    if s:
        print(s)
    else:    
        print("\tNo match")
    print

def research(input, regex):
    print("regex.search(%s)" % (input))
    s = regex.search(input)
    if s:
        print "\ts.group(): ",
        print s.group()
        print "\ts.groups(): ",
        print s.groups()
        print "\ts.start(), s.end(): ",
        print s.start(), s.end()
    else:    
        print("No match")
    print

def rematch(input, regex):
    print("regex.match(%s)" % (input))
    s = regex.match(input)
    if s:
        print "s.group(): ",
        print s.group()
        print "s.groups(): ",
        print s.groups()
        print "s.start(), s.end(): ",
        print s.start(), s.end()
    else:    
        print("No match")
    print

regex = raw_input("Regex to test : ")
input = raw_input("String to test : ")
print
if input and regex:
    try:
        p = re.compile(regex)
        good = 1
    except re.error, e:
        good = 0 
    if good:
        rematch(input, p)
        research(input, p)
        resplit(input, p)
        resub(input, 'FOO', p)
        resubn(input, 'FOO', p)
        refindall(input, p)
        refinditer(input, p)
